#!/usr/bin/python

# Flag para teste unitario
flag_unit_test=0
#flag_unit_test=1

# teste unitario
if flag_unit_test==1:
	import sys
	sys.path.append("lib/log")
 
import os
import MySQLdb
from log import Log
c=0

class DadosReal:

	def __init__(self):

		self.log = Log()
		self.log.update('DadosReal iniciada...\n')
		self.log.update('###################\n')
		# Open file configuration
		str_file = open('/tmp/db.cfg', 'r').read()
		conexao=str_file.split('\n')
		self.servidor=conexao[4].split('=')[1]
		self.usuario=conexao[5].split('=')[1]
		self.senha=conexao[6].split('=')[1]
		self.database=conexao[7].split('=')[1]
			
		# Open database connection
		self.db = MySQLdb.connect(self.servidor, self.usuario, self.senha, self.database )
		self.query=""
		#self.executeQuery()

	def executeQuery(self):

		#global c
		#c=c+1
		#self.log.update('c: ' + str(c) + '\n')
		
		# prepare a cursor object using cursor() method
		cursor = self.db.cursor()

		try:
			#self.log.update('Executando Query: ' + self.query + ":" + str(self.data) + '\n')
			#self.log.update('Executando Query: ' + self.query + '\n')
			# execute SQL query using execute() method.
			cursor.execute(self.query)
			print "%d row(s) were updated<br/>" % cursor.rowcount

			# Fetch a single row using fetchone() method.
			self.data = cursor.fetchall()
			cursor.close()
			self.db.commit()

		except MySQLdb.Error, e:
			self.log.update('Fail getting query [' + self.query + ']...\n')
     			self.log.update("Error %d: %s\n" % (e.args[0], e.args[1]))
			self.db.rollback()

	def openDados(self):
		# connect to server
		self.db = MySQLdb.connect(self.servidor, self.usuario, self.senha, self.database )

	def closeDados(self):
		# disconnect from server
		self.db.close()

	def getDados(self):

		return self.data

	def setQuery(self,s):

		self.query=s

	def getExecutor(self,i):

		self.query="SELECT * FROM executor where id_sistema=" + str(i) +";"
		self.executeQuery()
		return self.data

	def getPathExecutor(self,i,s):

		self.query="SELECT path FROM executor where id_executor=" + str(i) +" and id_sistema=" + str(s) + ";"
		self.executeQuery()
		return self.data[0][0]

	def getTarefas(self,i):

		self.query="SELECT * FROM tarefa where id_executor=" + str(i) +";"
		self.executeQuery()
		return self.data

	def getTarefa(self,id_executor, id_tarefa):

		self.query="SELECT nome FROM tarefa where id_executor=" + str(id_executor) + " and id_tarefa=" + str(id_tarefa) + ";"
		self.executeQuery()
		#self.data=["ls","-lthr"]
		return self.data[0][0]

	def getTarefaParametros(self, it, ie, s):

		self.query="Call getTarefaParametros(" + str(it) + "," + str(ie) + "," + str(s) + ");"
		self.executeQuery()
		return self.data[0][0]

	def setTarefaStatus(self ,it ,ie , ip, s):

		self.query="INSERT INTO tarefa_status (id_processamento, id_executor, id_tarefa, status) VALUES ( " + str(ip) + " ," + str(ie) + " ," + str(it) + " ," + str(s) + ") ON DUPLICATE KEY UPDATE status=" + str(s) + ";"
		self.executeQuery()
#teste local
if flag_unit_test==1:
	teste = DadosReal()
	#a=teste.getEntradas()
	#print a
	#print a[0][2]
	#a=teste.getProcessamento()
	#print a
	a=teste.getTarefa(1)
	print a
	
